﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier_Interface;
using Cashier.utils;
using Cashier_Utils;
using Cashier_Service;
using Cashier.prints;
using Cashier_Model;
using System.Collections;

namespace Cashier.pos
{
    public partial class PrintTicketForm : Form, IDataGridViewData
    {
        private MainForm mainform;
        private BillShipmentService shipmentService;
        private BillShipmentGoodsService shipmentGoodsService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mainform"></param>
        public PrintTicketForm(MainForm mainform)
        {
            InitializeComponent();
            this.dataGridView1.ImplementsInstance = this;
            this.mainform = mainform;
            this.shipmentService = new BillShipmentService();
            this.shipmentGoodsService = new BillShipmentGoodsService();

            this.RefreshDataGridViewData();
        }

        #region IDataGridViewData 成员

        void IDataGridViewData.EnterkeyEventHandler()
        {
            return;
        }

        void IDataGridViewData.GenerateGridViewColumns(DataGridView dgv)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("id", null, true, 0, null, null, false, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("shipment_no", "出货单号", true, 23, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("numbers", "商品数量", true, 12, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("goods_total", "商品总金额", true, 13, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("shipment_total", "帐单总金额", true, 13, ActKey.RIGHT, ActKey.RIGHT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("member_no", "购买客户", true, 13, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("admin_name", "出货人", true, 13, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("shipment_date", "出货日期", true, 13, ActKey.CENTER, ActKey.CENTER, true, false));
        }

        void IDataGridViewData.GetBindingData(int rowindex, int pageindex, int pagesize)
        {
            try
            {
                String shipmentno = textbox_shipmentno.Text.Trim();
                String adminUsername = mainform.UsernameForQuery;
                String startdate = String.Empty;
                String enddate = String.Empty;

                DataTable dt = shipmentService.GetBillShipmentTable(pageindex, pagesize, mainform.Admin.EnterpriseNo, String.Empty, shipmentno, String.Empty,
                    adminUsername, startdate, enddate, 0M, null, "id");
                this.dataGridView1.DataSource = dt;
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (rowindex > 0)
                        dataGridView1.Rows[0].Selected = false;
                    dataGridView1.Rows[rowindex].Selected = true;
                }
                dataGridView1.Columns[0].Visible = false;
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        long IDataGridViewData.GetBindingDataRecord()
        {
            long record = 0;
            try
            {
                String adminUsername = mainform.UsernameForQuery;
                String shipmentno = textbox_shipmentno.Text.Trim();
                String startdate = String.Empty;
                String enddate = String.Empty;

                record = shipmentService.GetBillShipmentRecord(mainform.Admin.EnterpriseNo, String.Empty, shipmentno, String.Empty, adminUsername, startdate, enddate, 0M);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
            return record;
        }

        #endregion

        #region 刷新数据

        /// <summary>
        /// 刷新数据
        /// </summary>
        public void RefreshDataGridViewData()
        {
            this.dataGridView1.LoadingData();
        }

        #endregion

        #region 功能方法实现

        /// <summary>
        /// 打印小票
        /// </summary>
        private void PrintPos()
        {
            if (printDocument1.PrinterSettings.IsValid)
            {
                if (Config.TICKET_PRINT_PREVIEW)
                {
                    PrintPreviewDialog previewDialog = new PrintPreviewDialog();
                    previewDialog.Document = printDocument1;
                    previewDialog.PrintPreviewControl.Zoom = 1;
                    previewDialog.ShowDialog();
                }
                else
                {
                    printDocument1.Print();
                }
            }
            else
            {
                MessageBox.Show("请配置打印机！");
            }
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_query_Click(object sender, EventArgs e)
        {
            this.RefreshDataGridViewData();
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_print_Click(object sender, EventArgs e)
        {
            this.PrintPos();
        }

        #endregion

        #region 打印事件

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                String shipmentno = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                BillShipment shipment = shipmentService.GetBillShipment(0L, shipmentno);
                ArrayList list = shipmentGoodsService.GetShipmentGoodsList(0, 0, mainform.Admin.EnterpriseNo, string.Empty, string.Empty, string.Empty, string.Empty,
                    shipmentno, string.Empty, string.Empty, string.Empty, string.Empty, "id", string.Empty);

                PosBillPrintDoc posBillPrintDoc = new PosBillPrintDoc(shipment, list);
                posBillPrintDoc.Print(shipmentno, mainform.Admin.Name + "  " + mainform.Admin.Mobile, e);
            }
        }

        #endregion

        #region 方法重写

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            { 
                case Keys.F8:
                    this.RefreshDataGridViewData();
                    break;
                case Keys.F9:
                    this.PrintPos();
                    break;
                case Keys.Up:
                    dataGridView1.Focus();
                    break;
                case Keys.Down:
                    dataGridView1.Focus();
                    break;
            }
            return b;
        }

        #endregion
    }
}
